#!/bin/bash

# iptables -t nat -A OUTPUT -d 192.168.0.22 -p tcp --dport 6443 -j DNAT --to-destination 172.20.150.48:6443
# 授权数据 资源文件路径 执行的操作

# JIANMU_COMMAND
# JIANMU_RESOURCE_FILE_PATH
# JIANMU_CERTIFICATE_AUTHORITY_DATA
# JIANMU_SERVER
# JIANMU_CLIENT_CERTIFICATE_DATA
# JIANMU_CLIENT_KEY_DATA
set -e

image_name="${JIANMU_REGISTRY}/ai/${JIANMU_JOB_NAME}:${JIANMU_NAMESPACE}-${JIANMU_BUILD_NUMBER}"
appconf="${JIANMU_APP_CONF}/${JIANMU_NAMESPACE}.${JIANMU_JOB_NAME}.yaml"

ls -l

generate_certificate() {
  mkdir -p "/root/.kube"
  python ./build_configuration.py
  echo "build kubeconfig complete"
}


deploy_resources() {
  # k8s部署
  deploymentfile=$(python ./exec.py "${JIANMU_JOB_NAME}" "${image_name}" "${JIANMU_NAMESPACE}" "${appconf}")
  logconfigfile=$(python ./logconfig.py "${JIANMU_JOB_NAME}" "${JIANMU_NAMESPACE}" "${appconf}")
  echo "deployment file:",$deploymentfile
  echo "logconfig file:",$logconfigfile
  kubectl --kubeconfig=/root/.kube/config apply -f $logconfigfile -n ai-${JIANMU_NAMESPACE}
  kubectl --kubeconfig=/root/.kube/config apply -f $deploymentfile -n ai-${JIANMU_NAMESPACE}
  echo "kube push complete"
}

generate_certificate
deploy_resources
